#include"RingQueue.hpp"
#include"Task.hpp"
#include<ctime>
#include<pthread.h>
#include<memory>
#include<sys/types.h>
#include<unistd.h>
#include<cstring>
using namespace std;
const char*ops="+-*/%";

void*consumerRoutine(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        Task t;
        rq->pop(&t);
        t();
        cout<<"consumer done,处理完成的任务是:"<<t.formatRes()<<endl;
        // int data=0;
        // rq->pop(&data);
        // cout<<"consumer done:"<<data<<endl;
        // sleep(1);

    }
}
void*productorRoutine(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        int x=rand()%100;
        int y=rand()%100;
        char op=ops[(x+y)%strlen(ops)];
        Task t(x,y,op);
        rq->push(t);
        cout<<"productor done,生产的任务是:"<<t.formatArg()<<endl;
        // int data=rand()%10+1;
        // rq->push(data);
        // cout<<"productor done:"<<data<<endl;
        // sleep(1);
    }

}
int main()
{
    srand(time(nullptr)^getpid());
    RingQueue<Task>*rq=new RingQueue<Task>();
    pthread_t c,p;
    pthread_create(&c,nullptr,consumerRoutine,rq);
    pthread_create(&p,nullptr,productorRoutine,rq);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);
    delete rq;

    return 0;
}